<?php

namespace app\common\model;

use think\Model;

/**
 * ============================================================================
 * 佳皿一站式,酒水服务类综合平台
 * ============================================================================
 * 版权所有 2019-2028 成都佳皿商贸有限公司，并保留所有权利。
 * 网站地址: http://www.91buys.com
 * ----------------------------------------------------------------------------
 * 最终解释权,归属于成都佳皿商贸有限公司。
 * 佳皿一站式服务虽好,别忘常来。
 * ============================================================================
 * 数据层模型
 */
class Tasting extends Model
{
    public $page_info;

    /**
     * 活动列表
     * @author csdeshang
     * @param type $condition   查询条件
     * @param type $page        分页页数
     * @param type $order       排序
     * @return type
     */
    public function gettastingList($condition, $page = '', $order = 'tasting_id asc') {
        if ($page) {
            $res = db('tasting')->where($condition)->order($order)->paginate($page, false, ['query' => request()->param()]);
            $this->page_info = $res;
            return $res->items();
        } else {
            return db('tasting')->where($condition)->order($order)->select();
        }
    }

    /**
     * 添加活动
     * @author csdeshang
     * @param type $data 查询数据
     * @return array 一维数组
     */
    public function addtasting($data)
    {
        return db('tasting')->insertGetId($data);
    }
    /**
     * 更新活动
     * @author csdeshang
     * @param type $data 活动数据
     * @param type $id   活动id
     * @return type
     */
    public function edittasting($data, $id)
    {
        return db('tasting')->where("tasting_id='$id' ")->update($data);
    }

    /**
     * 删除活动
     * @author csdeshang
     * @param type $condition 删除条件
     * @return type
     */
    public function deltasting($condition)
    {
        return db('tasting')->where($condition)->delete();
    }

    /**
     * 根据id查询一条活动
     * @author csdeshang
     * @param int $id 活动id
     * @return array 一维数组
     */
    public function getOnetastingById($id)
    {
        return db('tasting')->where('tasting_id',$id)->find();
    }

    public function getmemberList($condition, $page = '', $order = 'ts_member_id desc',$limit = '') {
        $field = 'tasting_member.ts_member_id,tasting_member.code,tasting_member.status,tasting_member.use_time,member.member_mobile member_name';
        if ($page) {
            $res = db('tasting_member')->alias('tasting_member')->where($condition)
                ->field($field)
                ->join('__MEMBER__ member','member.member_id=tasting_member.member_id','left')
                ->order($order)->paginate($page, false, ['query' => request()->param()]);
            $this->page_info = $res;
            $data =  $res->items();
        } else {
            $data = db('tasting_member')->alias('tasting_member')->where($condition)
                ->field($field)
                ->join('__MEMBER__ member','member.member_id=tasting_member.member_id','left')->select();
        }
        if($data){
            foreach ($data as &$item){
                $item['use_time'] = $item['use_time'] ? date('Y-m-d H:i:s',$item['use_time']) :'';
                switch ($item['status']){
                    case 0:
                        $item['status']  = '未使用';
                        break;
                    case 1:
                        $item['status']  = '已使用';
                        break;
                    case 2:
                        $item['status']  = '已过期';
                        break;
                }
            }
            return $data;
        }
        return $data;
    }
}